package algebra.rewriting.transform;

import algebra.op.AlgebraicOP;
import algebra.op.JoinOP;
import algebra.rewriting.RewriteRule;

public class AssociativeIntersectionF extends RewriteRule {

	private final AlgebraicOP op;

	public AssociativeIntersectionF(AlgebraicOP op) {
		this.op = op;
	}

	@Override
	public AlgebraicOP rewrite() {
		JoinOP ip = (JoinOP) op;
		JoinOP lp = ip.getFirstSubQuery().asIntersectionOP();
		AlgebraicOP rp = ip.getSecondSubQuery();
		return new JoinOP(lp.getFirstSubQuery(), new JoinOP(
				lp.getSecondSubQuery(), rp));
	}

	@Override
	public boolean applicable() {
		if (this.op instanceof JoinOP) {
			JoinOP ip = op.asIntersectionOP();
			if (ip.getFirstSubQuery() instanceof JoinOP) {
				return true;
			}
		}
		return false;
	}

}
